package top.suven.base.http.admin.user.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.user.model.UserData;
import top.suven.base.http.admin.user.service.UserDataWebService;
import top.suven.base.http.admin.user.vo.request.UserDataRequestVo;
import top.suven.base.http.admin.user.vo.response.UserDataResponseVo;
import top.suven.base.http.admin.user.vo.valid.UserValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**   
 * @Title: UserDataWebController.java
 * @Description: 用户扩展信息的控制服务类
 * @author lixiangling
 * @date   2018/08/14 16:52:42
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *用户扩展信息
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/user/userData")
public class UserDataWebController {
    private final static Logger logger = LoggerFactory.getLogger(UserDataWebController.class);
	@Autowired
	private UserDataWebService  userDataWebService;
    /**
     * @Title: 跳转到用户扩展信息主界面
     * @Description:userDataRequestVo @{Link UserDataRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/14 16:52:42
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    @RequiresPermissions("user:userData_list")
    public String index(){
        return "user/userData_index";
    }
    /**
     * @Title: 获取用户扩展信息分页信息
     * @Description:userDataRequestVo @{Link UserDataRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/14 16:52:42
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    @RequiresPermissions("user:userData_list")
    public PageData<UserDataResponseVo> list(@Validated(value = {UserValidGroup.UserQueryGroup.class}) UserDataRequestVo userDataRequestVo){
        UserData userData = new UserData();
        BeanUtil.copyProperties(userDataRequestVo, userData);
        return userDataWebService.getUserDataPage(userData, userDataRequestVo.getPage(), userDataRequestVo.getLimit());
    }
   /**
    * @Title: 新增用户扩展信息信息
    * @Description:userDataRequestVo @{Link UserDataRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "add")
    @RequiresPermissions("user:userData_add")
    public ResponseResult add(@Validated(value = {UserValidGroup.UserFormGroup.class}) UserDataRequestVo userDataRequestVo){
        UserData userData = new UserData();
        BeanUtil.copyProperties(userDataRequestVo, userData);
        userData.setStatus(StatusEnum.VALID.getIndex());
        userData.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
        userDataWebService.addUserData(userData);
        return ResponseResult.ok();
    }
    /**
    * @Title: 修改用户扩展信息信息
    * @Description:userDataRequestVo @{Link UserDataRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "modify")
    @RequiresPermissions("user:userData_modify")
    public ResponseResult modify(@Validated(value = {UserValidGroup.UserFormGroup.class}) UserDataRequestVo userDataRequestVo){
        UserData userData = new UserData();
        BeanUtil.copyProperties(userDataRequestVo, userData);
        if(userData.getUserId() == null){
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_UPD_INFO_NO_EXIST);
        }
        userDataWebService.updUserData(userData);
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看用户扩展信息信息
    * @Description:userDataRequestVo @{Link UserDataRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    @RequiresPermissions("user:userData_list")
    public String detail(ModelMap modelMap, Long id){
        UserData userData = null;
        if(id > 0){
            userData = userDataWebService.getUserData(id);
        }
        modelMap.put("userData", userData);
        return "user/userData_detail";
    }
    /**
     * @Title: 跳转用户扩展信息编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/14 16:52:42
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    @RequiresPermissions("user:userData_modify")
    public String edit(ModelMap modelMap, @RequestParam(defaultValue = "0") Long id){
        UserData userData = null;
        if(id > 0){
            userData = userDataWebService.getUserData(id);
        }
        modelMap.put("userData", userData);
        return "user/userData_edit";
    }
    /**
    * @Title: 跳转用户扩展信息新增编辑界面
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "newInfo")
    @RequiresPermissions("user:userData_add")
    public String newInfo(ModelMap modelMap){
        return "user/userData_edit";
    }
    /**
     * @Title: 删除用户扩展信息信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/14 16:52:42
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    @RequiresPermissions("user:userData_del")
    public ResponseResult del( @RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userDataWebService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用用户扩展信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    @RequiresPermissions("user:userData_turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userDataWebService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用用户扩展信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    @RequiresPermissions("user:userData_turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") Long[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        userDataWebService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出用户扩展信息信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/14 16:52:42
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    @RequiresPermissions("user:userData_export")
    public void export(HttpServletResponse response, UserDataRequestVo userDataRequestVo){
        UserData userData = new UserData();
        BeanUtil.copyProperties(userDataRequestVo, userData);
        PageData<UserDataResponseVo> userDataPage = userDataWebService.getUserDataPage(userData, userDataRequestVo.getPage(), userDataRequestVo.getLimit());
        List<UserDataResponseVo> data = userDataPage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("userId", "用户ID")
			.add("nationalityId", "国家ID")
			.add("provinceId", "省份ID")
			.add("cityId", "城市ID")
			.add("enableStatus", "启用状态")
			.add("status", "状态")
			.add("creater", "创建人")
			.add("createTime", "创建时间")
			.add("modifier", "修改人")
			.add("modifyTime", "修改时间")
			.add("reorder", "排序")
			.add("remark", "描述")
			.add("channelId", "")
			.add("channelIdOld", "")
			.add("baiduUserId", "")
			.add("baiduUserIdOld", "")
			.add("userEmail", "会员邮箱")
			.add("macId", "")
			.add("imei", "记录手机的IMEI码")
			.add("deviceToken", "")
			.add("imageUrl", "")
			.add("nickName", "")
			.add("lotteryCount", "可抽奖次数")
			.add("bankUsername", "开户人姓名")
			.add("bankCard", "银行卡号")
			.add("bankName", "银行名称")
			.add("contactPhone", "联系电话")
			.add("securePwd", "安全口令")
			.add("invitAwardLottery", "给邀请人奖励抽奖次数")
			.add("systemLotteryCount", "")
			.add("isClickHome", "")
			.add("sex", "1男2女")
			.add("birthday", "生日")
			.add("userLocation", "所在地")
			.add("userOccupation", "职业")
			.add("qualifications", "学历")
			.add("countyId", "区县id")
			.add("secureFaultCount", "svip退款安全口令错误次数")
			.add("padUnique", "用户手机唯一标识")
			.add("inviteCode", "邀请码，用于国庆邀请活动")
			.add("loginDays", "累积登陆天数")
			.add("cuid", "cuid")
			.add("secureFaultCount2", "")
			.add("secureFaultCount3", "svip退款安全口令错误次数");

        try {
            ExportUtils.write(String.format("用户扩展信息_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}